package com.mooc.comment.Dao;

import com.mooc.comment.common.RestResponse;
import com.mooc.comment.config.GenericRest;
import com.mooc.comment.model.User;
import com.mooc.comment.utils.Rests;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;


@Service
public class UserDao {

    @Autowired
    private GenericRest rest;
    @Value("${user.service.name}")
    private String userServiceName;


    public User getUserById(Long userId) {
        return Rests.exc(() ->{
            String url = Rests.toUrl(userServiceName, "/user/getUserById?id=" + userId);
            ResponseEntity<RestResponse<User>> responseEntity =
                    rest.get(url, new ParameterizedTypeReference<RestResponse<User>>() { });
            return responseEntity.getBody();
        }).getResult();
 }
}
